/*
 * ConfigReader.java
 *
 * Created on 29 March 2012, 14:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Simulation.Config.Service;

import Simulation.Utils.StringUtils;
import java.io.*;
import java.net.URL;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;

/**
 *
 * @author aking2
 */
public class ServiceConfigReader {
    
    public String GetData(String key) {
        for(int i=0; i<numOfDatums; i++) {
            if(key.equalsIgnoreCase(keyStore[i])) {
                return datumStore[i];
            }
        }
        return "UNKNOWN";
    }    
    
    /** Creates a new instance of ConfigReader */
    public ServiceConfigReader(String fileName) {
        
        // Create Storage Space
        keyStore   = new String[MAX_NUM_OF_DATUMS];
        datumStore = new String[MAX_NUM_OF_DATUMS];   
        numOfDatums = 0;
        
            
        URL               url;
        ClassLoader       loader;
        InputStream       is  = null;
        BufferedReader    br  = null;
        DataInputStream   dis = null;
        InputStreamReader isr = null;
        String            strLine;
        StringTokenizer   st;
        StringTokenizer   st2;
        String            token1;
        String            token2;

        try {
            
            /*
             * Create a buffered reader for the config file
             */
            loader = getClass().getClassLoader();        
            url = loader.getResource(fileName);
            is  = url.openStream();
            dis = new DataInputStream(is);    
            isr = new InputStreamReader(dis);        
            br  = new BufferedReader(isr);            
            
            /**
             * Keep reading lines from the config file until we get an EOF
             */
            while(true)   {
                
                strLine = br.readLine();
                
                /**
                 * Look out for the end of the file
                 */
                if (strLine == null) {
                    break;
                }
                
                /**
                 * Look out for blank lines
                 */
                if (StringUtils.IsWhiteSpace(strLine)) {
                    continue;
                }                   
                
                /**
                 * Configuration lines should have 2 tokens (name & value).
                 */
                st = new StringTokenizer(strLine);
                if (st.countTokens() == 2) {                    
                    token1 = st.nextToken();
                    token2 = st.nextToken(); 
                    
                    if( numOfDatums < MAX_NUM_OF_DATUMS ) {
                        keyStore[numOfDatums] = token1;
                        datumStore[numOfDatums] = token2;  
                        numOfDatums = numOfDatums + 1;
                    }                    
                }
            }
        } catch (IOException e) {
        } finally {
            
            /**
             * Close the buffer.
             */
            try {
                if(br != null)
                    br.close();
                if(isr != null)
                    isr.close();
                if(dis != null)
                    dis.close();
                if(is != null)
                    is.close();    
            } catch (IOException e) {
            }    
        }
    }          
    
    private static final int MAX_NUM_OF_DATUMS = 50;    
    private String[] keyStore;
    private String[] datumStore;    
    private int numOfDatums;
    
}
